package com.nopay.cdxphone.custom_view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import com.nopay.cdxphone.R;
import com.nopay.cdxphone.ui.adapter.OneTextviewStringAdapter;


/**
 * author : 底部弹出popupwindow，选择功能
 */
public class BottomPopupWindow {

    private Activity activity;

    private Dialog photoDialog;

    public BottomPopupWindow(Activity activity) {
        this.activity = activity;
    }

    public void showDialog(final List<String> list) {

        photoDialog = new Dialog(activity, R.style.MyDialog);
        Window window = photoDialog.getWindow();
        window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
        window.setWindowAnimations(R.style.mystyle3); // 添加动画

        View view = View.inflate(activity, R.layout.popup_function_list, null);

        RecyclerView rv = view.findViewById(R.id.rv_popup_function_list);
        rv.setLayoutManager(new LinearLayoutManager(activity));
        OneTextviewStringAdapter adapter = new OneTextviewStringAdapter(list);
        rv.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (!activity.isFinishing()) {
                    onItemChooseFunction(position ,list.get(position));
                    photoDialog.dismiss();
                }
            }
        });

        TextView tvCacel = view.findViewById(R.id.tv_popup_quxiao);
        tvCacel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onCancel();
                photoDialog.dismiss();
            }
        });

        photoDialog.setContentView(view);
        photoDialog.setCanceledOnTouchOutside(true);
        photoDialog.show();
        WindowManager.LayoutParams lp = photoDialog.getWindow().getAttributes();
        lp.width = ((WindowManager)(activity.getSystemService(Context.WINDOW_SERVICE))).
                getDefaultDisplay().getWidth();// 设置宽度
        photoDialog.getWindow().setAttributes(lp);
    }

    /**
     * 列表的条目点击事件
     *
     * @param position 选择的功能的位置
     * @param s
     */
    public void onItemChooseFunction(int position, String s) {

    }

    public void onCancel(){

    }
}
